VFG.DrawCircle=function(viewer,option){
	this.viewer=viewer;
	this.option=option;
	this.id=option.id;
	this.thing=option.thing;
	this.position=null;
	this.tip=new VFG.Tip(viewer);
	this.enity;
	this.positions=[];
	this.init();
	this.LEFT_CLICK_TIMES=0;
}

VFG.DrawCircle.prototype.init=function() {
	let _this=this;
	this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
	this.handler.setInputAction(function (movement) {
		let cartesian3=VFG.Util.getScreenToC3 (_this.viewer, movement.position, _this.enity) 
		if(Cesium.defined(cartesian3)){
			if (_this.positions.length == 0) {
	        	_this.positions.push(cartesian3.clone());
	        }
			_this.positions.push(cartesian3);
			_this.LEFT_CLICK_TIMES+=1;
			if(_this.LEFT_CLICK_TIMES>=2 ){
				if(_this.success){
					_this.positions.pop();
					_this.success(_this.getLnlas());
				}
				_this.stop();
			}
		}
    },Cesium.ScreenSpaceEventType.LEFT_CLICK)
    this.handler.setInputAction(function (movement) {
    	if (_this.LEFT_CLICK_TIMES == 0){
    		_this.tip.showAt(movement.endPosition,"单击开始绘制，右键退出");
    	}else{
    		_this.tip.showAt(movement.endPosition,"单击结束绘制，右键退出");
    	}
    	if(_this.LEFT_CLICK_TIMES > 0){
    		let cartesian3=VFG.Util.getScreenToC3 (_this.viewer, movement.endPosition, _this.enity) 
            if(Cesium.defined(cartesian3)){
            	_this.positions.pop();
            	_this.positions.push(cartesian3);
            }
    	}
    	if(_this.positions.length >= 2){
			if(!_this.entity){
	            _this.createEntity();
			}
		}
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	this.handler.setInputAction(function (movement) {
		_this.destroy();
	}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},

VFG.DrawCircle.prototype.addListener=function(type,callback){
	if('success'==type){
		this.success=callback;
	}
	if('error'==type){
		this.error=callback;
	}
	if('finish'==type){
		this.finish=callback;
	}
}

VFG.DrawCircle.prototype.removeListener=function(type){
	if('success'==type){
		this.success=null;
	}
	if('error'==type){
		this.error=null;
	}
	if('finish'==type){
		this.finish=null;
	}
}

VFG.DrawCircle.prototype.createEntity=function(){
	var _this=this;
	var geodesic = new Cesium.EllipsoidGeodesic();
	this._callbackProperty=function(){
        var satrt = Cesium.Cartographic.fromCartesian(_this.positions[0]);
        var end = Cesium.Cartographic.fromCartesian(_this.positions[_this.positions.length-1]);                   
        geodesic.setEndPoints(satrt, end);
        return geodesic.surfaceDistance.toFixed(3)*1
	};
	
	_this.entity =_this.viewer.entities.add({
		id:VFG.Util.getUuid(),
		position:_this.positions[0],
/*		point: {
           color: Cesium.Color.YELLOW,
           pixelSize: 5,
        },*/
		ellipse:{
        	semiMajorAxis:new Cesium.CallbackProperty(_this._callbackProperty, false),
        	semiMinorAxis:new Cesium.CallbackProperty(_this._callbackProperty, false),
            material: Cesium.Color.RED.withAlpha(0.4),
            outline: true,
            outlineColor: Cesium.Color.YELLOW,
            outlineWidth:2,
        }
    });
}

VFG.DrawCircle.prototype.stop = function() {
    if( this.handler){
    	this.handler.destroy();
    	this.handler = null;
    };
    if(this.tip){
    	this.tip.destroy();
    }
};

VFG.DrawCircle.prototype.getLnlas = function() {
	return [VFG.Util.getC3ToLnLa(this.viewer,this.positions[0]),VFG.Util.getC3ToLnLa(this.viewer,this.positions[1])];
};

VFG.DrawCircle.prototype.destroy = function() {
	if(this.finish){
		this.finish();
	}
    if( this.handler){
    	this.handler.destroy();
    	this.handler = null;
    };
    if(this.tip){
    	this.tip.destroy();
    }
	if(this.entity){
		this.viewer.entities.remove(this.entity);
	}
	this._callbackProperty=null;
	delete this.entity;
	delete this.tip;
	delete this._callbackProperty;
	delete this.handler;
    delete this.viewer;
	return Cesium.destroyObject(this);
};
